package com.auxgroup.smarthome.inner.entity;


import com.auxgroup.bridge.app.inner.dto.AppUserDto;
import com.auxgroup.bridge.app.inner.vo.AppUserVo;
import com.auxgroup.smarthome.BeanUtils;
import com.auxgroup.smarthome.basebean.Gender;

import javax.persistence.*;

/**
 * Created by kevinchen on 2016/12/20.
 */
@Entity
@Table(name = "app_user_info",indexes = {@Index(columnList = "gender", name = "index_app_user_gender"),
        @Index(columnList = "city", name = "index_app_user_city")})
public class AppUserEntity {

    @Id
    private String uid;
    //用户昵称
    private String nickName;
    //真实姓名
    private String realName;

    private String email;

    @Column(name = "phone", length = 20)
    private String phone;

    //用户头像URL地址
    private String headImg;
    /**
     * 索引字段，尽量不允许为空
     */
    @Column(length = 5,nullable = false)
    private String gender = Gender.N.name();

    private String birthday;
    //国家
    private String country = "UNKNOWN";
    //（ 中国）省、（美国）州、（泰国）府
    private String region  = "UNKNOWN";
    /**
     * 索引字段，尽量不允许为空
     */
    @Column(length = 50,nullable = false)
    private String city = "UNKNOWN";
    //终端设备唯一标识
    private String clientId;
    //上次登陆IP
    private String lastLoginIp;
    //上一次登录时间
    private Integer lastLoginTime;

    private Long createdAt;

    //微信id
    private String openid;

    //qqid
    private String qqid;

    //unionid
    private String unionid;

    //第三方帐号(京东/苏宁等)
    private String otherUserId;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getHeadImg() {
        return headImg;
    }

    public void setHeadImg(String headImg) {
        this.headImg = headImg;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getLastLoginIp() {
        return lastLoginIp;
    }

    public void setLastLoginIp(String lastLoginIp) {
        this.lastLoginIp = lastLoginIp;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Integer lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public Long getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Long createdAt) {
        this.createdAt = createdAt;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public String getQqid() {
        return qqid;
    }

    public void setQqid(String qqid) {
        this.qqid = qqid;
    }

    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    public String getOtherUserId() {
        return otherUserId;
    }

    public void setOtherUserId(String otherUserId) {
        this.otherUserId = otherUserId;
    }

    @PrePersist
    public void prePersist() {
        this.createdAt = System.currentTimeMillis()/1000;
    }

    public AppUserEntity() {
    }

    public AppUserEntity(String uid, String phone) {
        this.uid = uid;
        this.phone = phone;
    }

    /**
     * 用户每次登录记录用户登录信息
     * @param clientId
     * @param ip
     * @return
     */
    public AppUserEntity recordLoginInfo(String clientId,String ip){
        this.clientId = clientId;
        this.lastLoginIp = ip;
        this.lastLoginTime = Long.valueOf(System.currentTimeMillis()/1000).intValue();
        return this;
    }

    /**
     * appUserVo显示实体 转 AppUserEntity 数据实体
     * @param appUserVo
     * @return
     */
    public AppUserEntity createAppUser(AppUserVo appUserVo){
        return BeanUtils.copyAttrs(this, appUserVo);
    }

    public AppUserEntity createAppUser(String uid, String phone) {
        this.uid = uid;
        this.phone = phone;
        this.realName = phone;
        return this;
    }

    /**
     * 创建qq帐号 20180410
     * @param uid
     * @param openid
     * @param nickName
     * @param headImg
     * @return
     */
    public AppUserEntity createWechatAppUser(String uid, String openid,String unionid,String nickName,String headImg) {
        this.uid = uid;
        this.openid = openid;
        this.nickName = nickName;
        this.realName = nickName;
        this.headImg = headImg;
        this.unionid = unionid;
        return this;
    }

    /**
     * 创建微信帐号 20180410
     * @param uid
     * @param openid
     * @param nickName
     * @param headImg
     * @return
     */
    public AppUserEntity createQqAppUser(String uid, String openid,String nickName,String headImg) {
        this.uid = uid;
        this.qqid = openid;
        this.nickName = nickName;
        this.realName = nickName;
        this.headImg = headImg;
        return this;
    }

    /**
     * 创建第三方云帐号帐号 20180613
     * @param otherUserId
     * @param phone
     * @return
     */
    public AppUserEntity createCloudUser(String otherUserId, String phone) {
        this.otherUserId = otherUserId;
        this.phone = phone;
        return this;
    }

    public AppUserEntity updateUser(AppUserDto appUserDto){
        if(appUserDto.getBirthday() != null){
            this.birthday = appUserDto.getBirthday();
        }
        if(appUserDto.getCity() != null){
            this.city = appUserDto.getCity();
        }
        if(appUserDto.getCountry() != null){
            this.country = appUserDto.getCountry();
        }
        if(appUserDto.getGender() != null){
            this.gender = appUserDto.getGender();
        }
        if(appUserDto.getHeadImg() != null){
            this.headImg = appUserDto.getHeadImg();
        }
        if(appUserDto.getNickName() != null){
            this.nickName = appUserDto.getNickName();
        }
        if(appUserDto.getRealName() != null){
            this.realName = appUserDto.getRealName();
        }
        if(appUserDto.getRegion() != null){
            this.region = appUserDto.getRegion();
        }
        if(appUserDto.getUnionid() != null){
            this.unionid = appUserDto.getUnionid();
        }
        return this;
    }

}
